package com.baizhi.controller;

import com.baizhi.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")//访问请求路径的前缀  相当于以前namespace的值
public class TestController {
    @RequestMapping("login")//访问请求路径的后缀  相当于以前action标签内的name属性值
    public String login(String username, String password, HttpSession session, Map map){
        System.out.println("springMVC控制器类启动了");
        System.out.println("username="+username);
        System.out.println("password="+password);
        //将登陆标识存进session作用域内
        session.setAttribute("login",username);
        List<User> users = new ArrayList<>();
        users.add(new User(1,"张三"));
        users.add(new User(2,"李四"));
        users.add(new User(3,"王五"));
        //将数据存进map中  底层其实就是存进了request作用域  到页面可以从${key}获取数据
        map.put("list",users);
        return "forward:/show.jsp";
    }
    @RequestMapping("register")
    public String register(User user){
        System.out.println("user="+user);
        return "redirect:/index.jsp";
    }
    @RequestMapping("removeAny")
    public String renmoveAny(int[] ids){
        for(int i=0;i<ids.length;i++){
            System.out.println("id="+ids[i]);
        }
        return "redirect:/index.jsp";
    }
}
